<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qquickimageprovider.cpp -->
  <title>QQuickImageProvider 类 | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td ><a href="qtquick-module.html">C++ 类</a></td><td >QQuickImageProvider</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#public-functions">公用函数</a></li>
<li class="level1"><a href="#reimplemented-public-functions">重新实现的公用函数</a></li>
<li class="level1"><a href="#details">详细信息</a></li>
<li class="level2"><a href="#an-example">An Example</a></li>
<li class="level2"><a href="#asynchronous-image-loading">Asynchronous Image Loading</a></li>
<li class="level2"><a href="#image-caching">Image Caching</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QQuickImageProvider 类</h1>
<!-- $$$QQuickImageProvider-brief -->
<p>The <a href="qquickimageprovider.html">QQuickImageProvider</a> class provides an interface for supporting pixmaps and threaded image requests in QML. <a href="#details">更多...</a></p>
<!-- @@@QQuickImageProvider -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign">头文件：</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QQuickImageProvider&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += quick</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Since:</td><td class="memItemRight bottomAlign">  Qt 5.0</td></tr><tr><td class="memItemLeft rightAlign topAlign">继承自：</td><td class="memItemRight bottomAlign"> <a href="../qtqml/qqmlimageproviderbase.html">QQmlImageProviderBase</a></td></tr><tr><td class="memItemLeft rightAlign topAlign"> 被继承：</td><td class="memItemRight bottomAlign"> <p><a href="qquickasyncimageprovider.html">QQuickAsyncImageProvider</a></p>
</td></tr></table></div><ul>
<li><a href="qquickimageprovider-members.html">所有成员的列表，包括继承成员</a></li>
</ul>
<a name="public-functions"></a>
<h2 id="public-functions">公用函数</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qquickimageprovider.html#QQuickImageProvider">QQuickImageProvider</a></b>(QQmlImageProviderBase::ImageType <i>type</i>, QQmlImageProviderBase::Flags <i>flags</i> = ...)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qquickimageprovider.html#dtor.QQuickImageProvider">~QQuickImageProvider</a></b>() override</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QImage </td><td class="memItemRight bottomAlign"><b><a href="qquickimageprovider.html#requestImage">requestImage</a></b>(const QString &amp;<i>id</i>, QSize *<i>size</i>, const QSize &amp;<i>requestedSize</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QPixmap </td><td class="memItemRight bottomAlign"><b><a href="qquickimageprovider.html#requestPixmap">requestPixmap</a></b>(const QString &amp;<i>id</i>, QSize *<i>size</i>, const QSize &amp;<i>requestedSize</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QQuickTextureFactory *</td><td class="memItemRight bottomAlign"><b><a href="qquickimageprovider.html#requestTexture">requestTexture</a></b>(const QString &amp;<i>id</i>, QSize *<i>size</i>, const QSize &amp;<i>requestedSize</i>)</td></tr>
</table></div>
<a name="reimplemented-public-functions"></a>
<h2 id="reimplemented-public-functions">重新实现的公用函数</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> virtual QQmlImageProviderBase::Flags </td><td class="memItemRight bottomAlign"><b><a href="qquickimageprovider.html#flags">flags</a></b>() const override</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QQmlImageProviderBase::ImageType </td><td class="memItemRight bottomAlign"><b><a href="qquickimageprovider.html#imageType">imageType</a></b>() const override</td></tr>
</table></div>
<ul>
<li class="fn">2 个公用函数继承自<a href="../qtqml/qqmlimageproviderbase.html#public-functions">QQmlImageProviderBase</a></li>
</ul>
<a name="details"></a>
<!-- $$$QQuickImageProvider-description -->
<div class="descr">
<h2 id="details">详细信息</h2>
<p>The <a href="qquickimageprovider.html">QQuickImageProvider</a> class provides an interface for supporting pixmaps and threaded image requests in QML.</p>
<p><a href="qquickimageprovider.html">QQuickImageProvider</a> is used to provide advanced image loading features in QML applications. It allows images in QML to be:</p>
<ul>
<li>Loaded using QPixmaps rather than actual image files</li>
<li>Loaded asynchronously in a separate thread</li>
</ul>
<p>To specify that an image should be loaded by an image provider, use the <b>&quot;image:&quot;</b> scheme for the URL source of the image, followed by the identifiers of the image provider and the requested image. For example:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">source</span>: <span class="string">&quot;image://myimageprovider/image.png&quot;</span> }

</pre>
<p>This specifies that the image should be loaded by the image provider named &quot;myimageprovider&quot;, and the image to be loaded is named &quot;image.png&quot;. The QML engine invokes the appropriate image provider according to the providers that have been registered through <a href="../qtqml/qqmlengine.html#addImageProvider">QQmlEngine::addImageProvider</a>().</p>
<p>Note that the identifiers are case-insensitive, but the rest of the URL will be passed on with preserved case. For example, the below snippet would still specify that the image is loaded by the image provider named &quot;myimageprovider&quot;, but it would request a different image than the above snippet (&quot;Image.png&quot; instead of &quot;image.png&quot;).</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">source</span>: <span class="string">&quot;image://MyImageProvider/Image.png&quot;</span> }

</pre>
<p>If you want the rest of the URL to be case insensitive, you will have to take care of that yourself inside your image provider.</p>
<a name="an-example"></a>
<h4 >An Example</h4>
<p>Here are two images. Their <code>source</code> values indicate they should be loaded by an image provider named &quot;colors&quot;, and the images to be loaded are &quot;yellow&quot; and &quot;red&quot;, respectively:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-column.html">Column</a></span> {
      <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">source</span>: <span class="string">&quot;image://colors/yellow&quot;</span> }
      <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">source</span>: <span class="string">&quot;image://colors/red&quot;</span> }
  }

</pre>
<p>When these images are loaded by QML, it looks for a matching image provider and calls its <a href="qquickimageprovider.html#requestImage">requestImage</a>() or <a href="qquickimageprovider.html#requestPixmap">requestPixmap</a>() method (depending on its <a href="qquickimageprovider.html#imageType">imageType</a>()) to load the image. The method is called with the <code>id</code> parameter set to &quot;yellow&quot; for the first image, and &quot;red&quot; for the second.</p>
<p>Here is an image provider implementation that can load the images requested by the above QML. This implementation dynamically generates <a href="../qtgui/qpixmap.html">QPixmap</a> images that are filled with the requested color:</p>
<pre class="cpp">

  <span class="keyword">class</span> ColorImageProvider : <span class="keyword">public</span> <span class="type"><a href="qquickimageprovider.html#QQuickImageProvider">QQuickImageProvider</a></span>
  {
  <span class="keyword">public</span>:
      ColorImageProvider()
                 : <span class="type"><a href="qquickimageprovider.html#QQuickImageProvider">QQuickImageProvider</a></span>(<span class="type"><a href="qquickimageprovider.html#QQuickImageProvider">QQuickImageProvider</a></span><span class="operator">::</span>Pixmap)
      {
      }

      <span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span> requestPixmap(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>id<span class="operator">,</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> <span class="operator">*</span>size<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> <span class="operator">&amp;</span>requestedSize) override
      {
         <span class="type">int</span> width <span class="operator">=</span> <span class="number">100</span>;
         <span class="type">int</span> height <span class="operator">=</span> <span class="number">50</span>;

         <span class="keyword">if</span> (size)
            <span class="operator">*</span>size <span class="operator">=</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(width<span class="operator">,</span> height);
         <span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span> pixmap(requestedSize<span class="operator">.</span>width() <span class="operator">&gt;</span> <span class="number">0</span> <span class="operator">?</span> requestedSize<span class="operator">.</span>width() : width<span class="operator">,</span>
                        requestedSize<span class="operator">.</span>height() <span class="operator">&gt;</span> <span class="number">0</span> <span class="operator">?</span> requestedSize<span class="operator">.</span>height() : height);
         pixmap<span class="operator">.</span>fill(<span class="type"><a href="../qtgui/qcolor.html">QColor</a></span>(id)<span class="operator">.</span>rgba());
         <span class="keyword">return</span> pixmap;
      }
  };

</pre>
<p>To make this provider accessible to QML, it is registered with the QML engine with a &quot;colors&quot; identifier:</p>
<pre class="cpp">

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {

      <span class="type"><a href="qquickview.html">QQuickView</a></span> view;
      <span class="type"><a href="../qtqml/qqmlengine.html">QQmlEngine</a></span> <span class="operator">*</span>engine <span class="operator">=</span> view<span class="operator">.</span>engine();
      engine<span class="operator">-</span><span class="operator">&gt;</span>addImageProvider(QLatin1String(<span class="string">&quot;colors&quot;</span>)<span class="operator">,</span> <span class="keyword">new</span> ColorImageProvider);
      view<span class="operator">.</span>setSource(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;imageprovider-example.qml&quot;</span>)));
      view<span class="operator">.</span>show();
      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>Now the images can be successfully loaded in QML:</p>
<p class="centerAlign"><img src="images/imageprovider.png" alt="" /></p><p>See the <a href="qtquick-imageprovider-example.html">Image Provider Example</a> for the complete implementation. Note that the example registers the provider via a <a href="../qtqml/qqmlextensionplugin.html">plugin</a> instead of registering it in the application <code>main()</code> function as shown above.</p>
<a name="asynchronous-image-loading"></a>
<h4 >Asynchronous Image Loading</h4>
<p>Image providers that support <a href="../qtgui/qimage.html">QImage</a> or Texture loading automatically include support for asychronous loading of images. To enable asynchronous loading for an image source, set the <code>asynchronous</code> property to <code>true</code> for the relevant <a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">Image</a>, <a href="qml-qtquick-borderimage.html">BorderImage</a> or <a href="qml-qtquick-animatedimage.html">AnimatedImage</a> object. When this is enabled, the image request to the provider is run in a low priority thread, allowing image loading to be executed in the background, and reducing the performance impact on the user interface.</p>
<p>To force asynchronous image loading, even for image sources that do not have the <code>asynchronous</code> property set to <code>true</code>, you may pass the <code>QQmlImageProviderBase::ForceAsynchronousImageLoading</code> flag to the image provider constructor. This ensures that all image requests for the provider are handled in a separate thread.</p>
<p>Asynchronous loading for image providers that provide <a href="../qtgui/qpixmap.html">QPixmap</a> is only supported in platforms that have the ThreadedPixmaps feature, in platforms where pixmaps can only be created in the main thread (i.e&#x2e; ThreadedPixmaps is not supported) if <a href="qml-qtquick-image.html#asynchronous-prop">asynchronous</a> is set to <code>true</code>, the value is ignored and the image is loaded synchronously.</p>
<p>Asynchronous image loading for providers of type other than <a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">ImageResponse</a> are executed on a single thread per engine basis. That means that a slow image provider will block the loading of any other request. To avoid that we suggest using <a href="qquickasyncimageprovider.html">QQuickAsyncImageProvider</a> and implement threading on the provider side via a <code>QThreadPool</code> or similar. See the <a href="qtquick-imageresponseprovider-example.html">Image Response Provider Example</a> for a complete implementation.</p>
<a name="image-caching"></a>
<h4 >Image Caching</h4>
<p>Images returned by a <a href="qquickimageprovider.html">QQuickImageProvider</a> are automatically cached, similar to any image loaded by the QML engine. When an image with a &quot;image://&quot; prefix is loaded from cache, <a href="qquickimageprovider.html#requestImage">requestImage</a>() and <a href="qquickimageprovider.html#requestPixmap">requestPixmap</a>() will not be called for the relevant image provider. If an image should always be fetched from the image provider, and should not be cached at all, set the <code>cache</code> property to <code>false</code> for the relevant <a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">Image</a>, <a href="qml-qtquick-borderimage.html">BorderImage</a> or <a href="qml-qtquick-animatedimage.html">AnimatedImage</a> object.</p>
</div>
<p><b>请参考 </b><a href="../qtqml/qqmlengine.html#addImageProvider">QQmlEngine::addImageProvider</a>()。</p>
<!-- @@@QQuickImageProvider -->
<div class="func">
<h2>成员函数文档</h2>
<!-- $$$QQuickImageProvider[overload1]$$$QQuickImageProviderQQmlImageProviderBase::ImageTypeQQmlImageProviderBase::Flags -->
<h3 class="fn" id="QQuickImageProvider"><a name="QQuickImageProvider"></a>QQuickImageProvider::<span class="name">QQuickImageProvider</span>(<span class="type"><a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">QQmlImageProviderBase::ImageType</a></span> <i>type</i>, <span class="type"><a href="../qtqml/qqmlimageproviderbase.html#Flag-enum">QQmlImageProviderBase::Flags</a></span> <i>flags</i> = ...)</h3>
<p>Creates an image provider that will provide images of the given <i>type</i> and behave according to the given <i>flags</i>.</p>
<!-- @@@QQuickImageProvider -->
<!-- $$$~QQuickImageProvider[overload1]$$$~QQuickImageProvider -->
<h3 class="fn" id="dtor.QQuickImageProvider"><a name="dtor.QQuickImageProvider"></a><code>[override virtual] </code>QQuickImageProvider::<span class="name">~QQuickImageProvider</span>()</h3>
<p>Destroys the <a href="qquickimageprovider.html">QQuickImageProvider</a></p>
<p><b>Note: </b>The destructor of your derived class need to be thread safe.</p><!-- @@@~QQuickImageProvider -->
<!-- $$$flags[overload1]$$$flags -->
<h3 class="fn" id="flags"><a name="flags"></a><code>[override virtual] </code><span class="type"><a href="../qtqml/qqmlimageproviderbase.html#Flag-enum">QQmlImageProviderBase::Flags</a></span> QQuickImageProvider::<span class="name">flags</span>() const</h3>
<p>重新实现自<a href="../qtqml/qqmlimageproviderbase.html#flags">QQmlImageProviderBase::flags</a>()。</p>
<p>Returns the flags set for this provider.</p>
<!-- @@@flags -->
<!-- $$$imageType[overload1]$$$imageType -->
<h3 class="fn" id="imageType"><a name="imageType"></a><code>[override virtual] </code><span class="type"><a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">QQmlImageProviderBase::ImageType</a></span> QQuickImageProvider::<span class="name">imageType</span>() const</h3>
<p>重新实现自<a href="../qtqml/qqmlimageproviderbase.html#imageType">QQmlImageProviderBase::imageType</a>()。</p>
<p>Returns the image type supported by this provider.</p>
<!-- @@@imageType -->
<!-- $$$requestImage[overload1]$$$requestImageconstQString&QSize*constQSize& -->
<h3 class="fn" id="requestImage"><a name="requestImage"></a><code>[virtual] </code><span class="type"><a href="../qtgui/qimage.html">QImage</a></span> QQuickImageProvider::<span class="name">requestImage</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>id</i>, <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> *<i>size</i>, const <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> &amp;<i>requestedSize</i>)</h3>
<p>Implement this method to return the image with <i>id</i>. The default implementation returns an empty image.</p>
<p>The <i>id</i> is the requested image source, with the &quot;image:&quot; scheme and provider identifier removed. For example, if the image <a href="qml-qtquick-image.html#source-prop">source</a> was &quot;image://myprovider/icons/home&quot;, the given <i>id</i> would be &quot;icons/home&quot;.</p>
<p>The <i>requestedSize</i> corresponds to the <a href="qml-qtquick-image.html#sourceSize-prop">Image::sourceSize</a> requested by an Image item. If <i>requestedSize</i> is a valid size, the image returned should be of that size.</p>
<p>In all cases, <i>size</i> must be set to the original size of the image. This is used to set the <a href="qml-qtquick-item.html#width-prop">width</a> and <a href="qml-qtquick-item.html#height-prop">height</a> of the relevant <a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">Image</a> if these values have not been set explicitly.</p>
<p><b>Note: </b>this method may be called by multiple threads, so ensure the implementation of this method is reentrant.</p><!-- @@@requestImage -->
<!-- $$$requestPixmap[overload1]$$$requestPixmapconstQString&QSize*constQSize& -->
<h3 class="fn" id="requestPixmap"><a name="requestPixmap"></a><code>[virtual] </code><span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span> QQuickImageProvider::<span class="name">requestPixmap</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>id</i>, <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> *<i>size</i>, const <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> &amp;<i>requestedSize</i>)</h3>
<p>Implement this method to return the pixmap with <i>id</i>. The default implementation returns an empty pixmap.</p>
<p>The <i>id</i> is the requested image source, with the &quot;image:&quot; scheme and provider identifier removed. For example, if the image <a href="qml-qtquick-image.html#source-prop">source</a> was &quot;image://myprovider/icons/home&quot;, the given <i>id</i> would be &quot;icons/home&quot;.</p>
<p>The <i>requestedSize</i> corresponds to the <a href="qml-qtquick-image.html#sourceSize-prop">Image::sourceSize</a> requested by an Image item. If <i>requestedSize</i> is a valid size, the image returned should be of that size.</p>
<p>In all cases, <i>size</i> must be set to the original size of the image. This is used to set the <a href="qml-qtquick-item.html#width-prop">width</a> and <a href="qml-qtquick-item.html#height-prop">height</a> of the relevant <a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">Image</a> if these values have not been set explicitly.</p>
<p><b>Note: </b>this method may be called by multiple threads, so ensure the implementation of this method is reentrant.</p><!-- @@@requestPixmap -->
<!-- $$$requestTexture[overload1]$$$requestTextureconstQString&QSize*constQSize& -->
<h3 class="fn" id="requestTexture"><a name="requestTexture"></a><code>[virtual] </code><span class="type"><a href="qquicktexturefactory.html">QQuickTextureFactory</a></span> *QQuickImageProvider::<span class="name">requestTexture</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>id</i>, <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> *<i>size</i>, const <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> &amp;<i>requestedSize</i>)</h3>
<p>Implement this method to return the texture with <i>id</i>. The default implementation returns <code>nullptr</code>.</p>
<p>The <i>id</i> is the requested image source, with the &quot;image:&quot; scheme and provider identifier removed. For example, if the image <a href="qml-qtquick-image.html#source-prop">source</a> was &quot;image://myprovider/icons/home&quot;, the given <i>id</i> would be &quot;icons/home&quot;.</p>
<p>The <i>requestedSize</i> corresponds to the <a href="qml-qtquick-image.html#sourceSize-prop">Image::sourceSize</a> requested by an Image item. If <i>requestedSize</i> is a valid size, the image returned should be of that size.</p>
<p>In all cases, <i>size</i> must be set to the original size of the image. This is used to set the <a href="qml-qtquick-item.html#width-prop">width</a> and <a href="qml-qtquick-item.html#height-prop">height</a> of the relevant <a href="../qtqml/qqmlimageproviderbase.html#ImageType-enum">Image</a> if these values have not been set explicitly.</p>
<p><b>Note: </b>this method may be called by multiple threads, so ensure the implementation of this method is reentrant.</p><!-- @@@requestTexture -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
